package com.yonyou.iuap.jxzy.servlet.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.iweb.exception.BusinessException;
import com.yonyou.iuap.jxzy.entity.PoundBillVO;
import com.yonyou.iuap.jxzy.servlet.BaedDeviceService;
import com.yonyou.iuap.jxzy.servlet.DeviceInfo;
import com.yonyou.iuap.jxzy.servlet.dao.DeviceMapper;
import com.yonyou.iuap.jxzy.utils.HttpRequestUtil;

/**
 * 刷卡控制led显示
 * @author 王格萍
 * @date 2019年6月21日
 *
 */
@Service
public class ControlLifeTOLEDService implements BaedDeviceService{

	public static final String LEDURL = "http://164.70.12.29:8080/unattended_hardware/hardwareOperation/showLedJX";

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DeviceMapper deviceMapper;

	@Override
	public void processor(DeviceInfo deviceinfo) throws BusinessException {
		//根据卡id获取卡号
		String cardid = (String) deviceinfo.getUserData();
		String cardno=this.deviceMapper.queryCardnoByCardid(cardid);//根据卡id获取卡号
		if(cardno!=null){
			//根据卡号获取未完成磅单的车牌号及总件数
			List<PoundBillVO>  poundvo=this.deviceMapper.queryPoundVde3ByCardno(cardno);
			if(poundvo!=null && poundvo.size()>0){
				String leddeviceCode=this.deviceMapper.getDivicecode(deviceinfo.getDeviceCode());//获取设备编码  deviceinfo.getDeviceCode()：刷卡编码
				String vvehicle=poundvo.get(0).getVvehicle();
				String vdef3=poundvo.get(0).getVdef3();//总件数
				if(leddeviceCode!=null && leddeviceCode.length()>0){
					JSONObject json=new JSONObject();
					json.put("deviceCode", leddeviceCode);
					String ledtext=vvehicle+"\n"+"总件数："+vdef3;
					json.put("ledText", ledtext);
					String result=HttpRequestUtil.sendPost2(LEDURL, json.toString());
					if("".equals(result)|| Boolean.FALSE.equals(new Boolean(result))){
						logger.error("LED显示异常");
						throw new BusinessException("LED显示异常");
					}
					//线程沉睡3秒，防止卡片未拿走
					try {
						Thread.sleep(3000);
						logger.error("led显示成功："+ledtext);
					} catch (InterruptedException e) {
						logger.error("线程异常："+e.getMessage());
						throw new BusinessException("线程异常："+e.getMessage());
					}
				}
				
			}
			
		}
	}
}
